package com.example.demo.database.mydbpool;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.Properties;

public class PoolConn {
	private static String jdbcDriver;

	private static String jdbcUrl;

	private static String username;

	private static String password;

	private static Integer initConnectCount;

	private static Integer maxConnects;

	private static Integer incrementCount;

	private static LinkedList<Connection> connections = new LinkedList<>();

	/**
	 * 通过实例初始化块来初始化
	 */
	static {
		// 读取对应的配置文件，加载入properties中，并设置到对应的参数中
		InputStream is = PoolConn.class.getClassLoader().getResourceAsStream("jdbc.properties");
		Properties properties = new Properties();
		try {
			properties.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
		jdbcDriver = properties.getProperty("jdbcDriver");
		jdbcUrl = properties.getProperty("jdbcUrl");
		username = properties.getProperty("username");
		password = properties.getProperty("password");
		initConnectCount = Integer.valueOf(properties.getProperty("initConnectCount"));
		maxConnects = Integer.valueOf(properties.getProperty("maxConnects"));
		incrementCount = Integer.valueOf(properties.getProperty("incrementCount"));

		try {
			/*
			 * 注册jdbc驱动
			 */
			Driver driver = (Driver) Class.forName(jdbcDriver).newInstance();
			DriverManager.registerDriver(driver);
			/*
			 * 根据initConnectCount来初始化连接池
			 */
			createConnections(initConnectCount);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	// 初始化线程池
	public static void createConnections(int initConnectCount) {
		for (int i = 0; i < initConnectCount; i++) {
			try {
				Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
				connections.addLast(connection);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		System.out.println("数据库创建完成：[" + connections.size() + "]");
	}

	public Connection getConn(int mils) throws InterruptedException {
		synchronized (connections) {
			if (mils < 0) {
				while (connections.isEmpty()) {
					connections.wait();
				}
				Connection removeFirst = connections.removeFirst();
				return removeFirst;
			} else {
				long overtime = System.currentTimeMillis() + mils;
				long time = mils;
				while (connections.isEmpty() && time > 0) {
					connections.wait(time);
//					time = overtime - System.currentTimeMillis();
				}
				Connection conn = null;
				if (!connections.isEmpty()) {
					conn = connections.removeFirst();
				}
				return conn;
			}
		}
	}

	public synchronized void released(Connection conn) {
		synchronized (connections) {

			System.out.println("数据库剩余连接数： [" + connections.size() + "]");
			if (conn != null) {
				connections.addLast(conn);
				connections.notifyAll();
			}
		}
	}

}
